// 导入表单验证插件
import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    houseId: '',
    houseName: '',
    repairItemId: '',
    repairItemName: '',
    mobile: '15660762022',
    description: '123',
    appointment: '',
    ////////////////////////////////////////
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: []
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' }
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }]
  },
  onLoad ({ id }) {
    if (id) this.getRepairDetail(id)
    this.getHouseList()
    this.getRepairItem()
  },
  async getHouseList () {
    const { data } = await wx.http.get('/house')
    this.setData({ houseList: data })
  },
  selectHouseInfo (ev) {
    console.log(ev)
    this.setData({ houseName: ev.detail.name, houseId: ev.detail.id })
    console.log(ev.detail.name)
  },
  // 获取维修项目
  async getRepairItem () {
    const { data } = await wx.http.get('/repairItem')
    this.setData({ repairItem: data })
  },
  selectRepairItemInfo (ev) {
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name
    })
  },
  selectDateInfo (ev) {
    const data = wx.utils.dateFormat(ev.detail)

    this.setData({ appointment: data, dateLayerVisible: false })
  },
  uploadPicture (ev) {
    const { file } = ev.detail
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: result => {
        const data = JSON.parse(result.data)
        console.log(data)
        const { attachment } = this.data
        attachment.push(data.data)
        this.setData({ attachment })
      }
    })
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },

  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  async submit () {
    const flag = this.validate()
    if (!flag) return
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast('在线报修失败!')
    // 跳转到报修列表页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },
  //修改房屋
  async getRepairDetail (id) {
    const { data: repairDetail } = await wx.http.get('/repair/' + id)
    this.setData({ ...repairDetail })
  }
})
